package com.itheima.day09泛型和二叉树;

import java.util.TreeSet;

/*2、【编号：1505】现有若干图书信息（包含名称title
、作者author、定价price）需要存储到set集合中，
保证集合中无重复元素，并遍历查看。
可以认为所有信息都相同的图书为重复数据。*/
public class Demo2 {
    public static void main(String[] args) {
        TreeSet<Book> set = new TreeSet<Book>();
        set.add(new Book("三国演义", "吴冠中", "22.5"));
        set.add(new Book("三国演义", "吴冠中", "22.5"));
        set.add(new Book("水浒传", "吴冠中", "23"));
        set.add(new Book("红楼梦", "吴冠中", "24"));
        set.add(new Book("三国演义", "吴冠中", "28"));
        set.add(new Book("三国演义", "吴冠中", "22.5"));
        for (Book book : set) {
            System.out.println(book);        }    }}

class Book implements Comparable<Book> {
    private String title;
    private String author;
    private String price;

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public Book(String title, String author, String price) {
        this.title = title;
        this.author = author;
        this.price = price;    }

    public Book() {    }

    @Override
    public int compareTo(Book o) {
        int result = this.getTitle().compareTo(o.getTitle());
        result = result == 0 ? this.getAuthor().compareTo(o.getAuthor()) : result;
        result = result == 0 ? this.getPrice().compareTo(o.getPrice()) : result;
        return result;
    }
}